package edu.yuhf.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.yuhf.db.utils.JdbcTemplate;

import edu.yuhf.dao.iface.NativePlaceDao;
import edu.yuhf.domain.NativePlace;

public class NativePlaceJdbcDaoImpl implements NativePlaceDao {

	@Override
	public List<NativePlace> getProvinces() {
		String sql="select * from nativeplace where length(code)=2";
		return JdbcTemplate.query(sql, ((rs)->{
			List<NativePlace> list=new ArrayList<NativePlace>();
			try {
				while(rs.next()) {
					NativePlace np=new NativePlace(rs.getInt(1),rs.getString(2),rs.getString(3));
					list.add(np);
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
			return list;
		}), new Object[] {});
	}

	@Override
	public List<NativePlace> getCitys(String provinceCode) {
		String sql="select * from nativeplace where length(code)=4 and substr(code,1,2)=?";
		return JdbcTemplate.query(sql, ((rs)->{
			List<NativePlace> list=new ArrayList<NativePlace>();
			try {
				while(rs.next()) {
					NativePlace np=new NativePlace(rs.getInt(1),rs.getString(2),rs.getString(3));
					list.add(np);
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
			return list;
		}), new Object[] {provinceCode});
	}

}
